<?php
namespace app\admin\controller;
use app\admin\controller\Common;
use app\service\propertyService;
use think\facade\Db;
use think\facade\View;

class Property extends Common{
    //物业管理
    public function propertyList(){
        $list = (new propertyService())->propertyList();
        //var_dump($list);
        $number = count($list);

        View::assign('number',$number);
        View::assign('list',$list);
        return view('property_list');
    } 
    public function propertyAdd(){
        if(request()->isPost()){
            //获取表单数据
            $data = request()->post();
            //判断账号名称是否重复        
            $info = (new propertyService())->propertyName($data['name']);
            if($info){
                $this->error('物业已存在');
            }
            $data['time'] = date('Y-m-d H:i:s',time());
            //插入数据到数据库
            $result = (new propertyService())->propertyAdd($data);
            if($result){
                $this->success('添加物业成功',url('propertyAdd'));
            }else{
                $this->error('添加物业失败',url('propertyAdd'));
            }             
        }
        return View::fetch('property_add');
    }
    public function propertyEdit(){
        if(request()->isGet()){
            $id = request()->param('id');
            if(!isset($id) && empty($id)){
                $this->error('物业不存在');
            }
            $info = (new propertyService())->propertyId($id);
            View::assign('info',$info);
            return View::fetch('property_edit');
        }else{
            $data = request()->post();
            $result = (new propertyService())->propertyEdit($data);
            if($result){
                    $this->success('修改物业成功',url('propertyList'));
            }else{
                    $this->error('修改物业失败');
            }
        }
    }
    public function propertyDel(){
        $id = request()->param('id');
		$validate = $this->validate(request()->param(),[
			'id'	=>	'require|number'
		]);
		if($validate !== true){
			$this->error($validate);
        }
        //判断是否存在该数据
		$info = (new propertyService())->propertyId($id);
		if(empty($info)){
			$this->error('该物业不存在');
		}
		$result =  (new propertyService())->propertyDel($id);
        if($result){
            $this->success('删除物业成功',url('propertyList'));
		}else{
            $this->success('删除物业失败',url('propertyList'));
		}
    }
}